﻿using COFE.Core.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace COFE.Core.Entry
{
    public class ResourceList : IResourceList
    {
        public ResourceList(object parentResource, IEnumerable<object> resources)
        {
            ParentResource = parentResource;
            _resources = new List<object>(resources);
        }

        public ResourceList(IPropertyHost propertyHost, object parentResource, 
            PropertyRetrivalOptions options = PropertyRetrivalOptions.NotApplied)
        {
            ParentResource = parentResource;

            var ra = EnumUtils<ResourceAttribute>.FindAttribute(parentResource);
            if (ra == null && ra.MajorProperty)
                throw new ArgumentException("Parent resource is not a major resource.");
            var rla = EnumUtils<ResourceListAttribute>.FindAttribute(parentResource);
            if (rla == null)
                throw new ArgumentException("Parent resource do not have ResourceListAttribute tagged.");
            _resources = new List<object>(propertyHost.GetSupportedProperty(options, rla.MatchMode, rla.Filters));
        }

        private List<object> _resources;

        public object ParentResource { get; private set; }

        public IEnumerable<object> Resources { get { return _resources; } }
        
    }
}
